<?php

declare(strict_types=1);

namespace Zyucv7\Components\DataAccess\Uxts\Expert;

use Zyucv7\Components\DataAccess\BaseModel\Uxt;

/**
 * 专家任务表
 */
class Option extends Uxt
{
    protected $table = 'exp_task_option';

    // 查询所有关联
    public function getParents()
    {

        $query = $this->newQuery();
        $query->where('parent_id', '=', 0);
        $rows = $query->getAll();
        $parents = [];
        foreach ($rows as $row) {
            // $parents[$row['id']] = $row['name'];
            $parents[$row['id']] = [
                'name' => $row['name'],
                'childId' => [],
                'count' => 0
            ];
        }

        $query = $this->newQuery();
        $query->where('parent_id', '>', 0);
        $rows = $query->getAll();
        $childs = [];
        foreach ($rows as $row) {
            if (!isset($childs[$row['parent_id']])) {
                $childs[$row['parent_id']] = [];
            }
            $childs[$row['parent_id']][] = $row['id'];
        }

        foreach ($parents as $key => $value) {
            if (isset($childs[$key])) {
                $parents[$key]['childId'] = $childs[$key] ?? [];
            }
        }
        

        return $parents;
    }
}
